package com.juyu.miliao.api.service;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.dao.redis.usercache.UserCacheVo;
import com.juyu.miliao.api.dao.redis.usercache.UserInfoCache;
import com.juyu.miliao.api.domain.Appkey;
import com.juyu.miliao.api.domain.Gift;
import com.juyu.miliao.api.domain.vo.SpringFestivalActivitiesVO;
import com.juyu.miliao.common.constant.SpringFestivalActivitiesGiftEnums;
import com.juyu.miliao.common.constant.SpringFestivalActivitiesRedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author xuanqicheng
 * @create 2019-28-13:53
 */
@Service
public class SpringFestivalActivitiesService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UserInfoCache userInfoCache;

    @Autowired
    private GiftService giftService;

    @Autowired
    private AppKeyService appKeyService;

    /**
     * 根据类型查看榜单
     *
     * @param userId
     * @param type
     * @return
     */
    public Object getGoldListByType(long userId, String type) {
        Map<String,Object> map = new HashMap<>(2);
        if (type.equals(SpringFestivalActivitiesGiftEnums.GIFT_FIRE_WORKS.getType())
                || type.equals(SpringFestivalActivitiesGiftEnums.GIFT_LUCKY_BAG.getType())
                || type.equals(SpringFestivalActivitiesGiftEnums.GIFT_YUAN_BO.getType())
                || type.equals(SpringFestivalActivitiesGiftEnums.GIFT_MAN_MON.getType())
                || type.equals(SpringFestivalActivitiesGiftEnums.GIFT_GOLDEN_PIG.getType())) {

            int giftId = SpringFestivalActivitiesGiftEnums.getGiftIdByType(type);

            Gift gift = giftService.getGiftById(giftId);

            String key = SpringFestivalActivitiesRedisKey.SPRING_FESTIVAL_ACTIVITIES_TYPE_KEY+type;

            setSpringFestivalActivitiesInfo(userId, map, gift.getIcon(), key);

        }else{

            String key = SpringFestivalActivitiesRedisKey.SPRING_FESTIVAL_ACTIVITIES_GOLD_LIST_KEY;

            Appkey appkey = appKeyService.getAppKeyByKey("springFestivalImg");

            setSpringFestivalActivitiesInfo(userId, map, appkey.getValue(), key);
        }

        return map;
    }

    private void setSpringFestivalActivitiesInfo(long userId, Map<String, Object> map, String giftImg, String key) {
        Set<ZSetOperations.TypedTuple<String>> set = stringRedisTemplate.opsForZSet()
                .reverseRangeWithScores(key, 0, 9);

        List<ZSetOperations.TypedTuple<String>> list = new ArrayList<>(set);

        List<SpringFestivalActivitiesVO> springFestivalActivitiesVOS = new ArrayList<>();


        for(int i=0;i<list.size();i++){
            ZSetOperations.TypedTuple<String> obj = list.get(i);

            long inUserId = Long.parseLong(obj.getValue());

            UserCacheVo inUserInfo = userInfoCache.getUserCacheInfo(inUserId);

            int num = obj.getScore().intValue();

            SpringFestivalActivitiesVO activitiesVO = new SpringFestivalActivitiesVO()
                    .setInUserId(inUserId)
                    .setInUserNickName(inUserInfo.getNickName())
                    .setInUserIcon(inUserInfo.getIcon())
                    .setSex(inUserInfo.getSex())
                    .setAge(inUserInfo.getAge())
                    .setMGrade(inUserInfo.getMGrade())
                    .setVGrade(inUserInfo.getVGrade())
                    .setNum(num)
                    .setGiftImg(giftImg)
                    .setSort(Integer.toString(i+1));

            springFestivalActivitiesVOS.add(activitiesVO);

        }

        map.put("list",springFestivalActivitiesVOS);

        UserCacheVo userInfo = userInfoCache.getUserCacheInfo(userId);
        //获取当前用户的排行榜信息
        SpringFestivalActivitiesVO userActivitiesVO = new SpringFestivalActivitiesVO()
                .setInUserId(userId)
                .setInUserNickName(userInfo.getNickName())
                .setInUserIcon(userInfo.getIcon())
                .setSex(userInfo.getSex())
                .setAge(userInfo.getAge())
                .setMGrade(userInfo.getMGrade())
                .setVGrade(userInfo.getVGrade())
                .setGiftImg(giftImg);


        Double tempNum = stringRedisTemplate.opsForZSet().score(key, Long.toString(userId));

        if(tempNum!=null){

            int myNum = tempNum.intValue();

            userActivitiesVO.setNum(myNum);

            Long sort = stringRedisTemplate.opsForZSet().reverseRank(key, Long.toString(userId));

            if(sort!=null && sort<100){

                userActivitiesVO.setSort(Long.toString(sort+1));

            }else{

                userActivitiesVO.setSort("100+");
            }
        }else{
            userActivitiesVO.setNum(0);
            userActivitiesVO.setSort("100+");
        }


        map.put("user",userActivitiesVO);
    }

}
